﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Remoting.Contexts;
using System.Text;
using System.Web;


/**
* @Author: AdinZ
* @Q  Q : 1454269
* @Email:adinz@adinz.com
* @Created: 2014/5/20 20:00:45
*
* */
namespace Reader.Api
{
    public class CallBack
    {

        private static string KEY = "5000139515C841D8";

        public static void Api(HttpContext context)
        {
            string Phone = context.Request["Phone"];
            string MchNo = context.Request["MchNo"];
            string Fee = context.Request["Fee"];
            string OrderId = context.Request["OrderId"];
            string Sign = context.Request["Sign"];
            string MobileType = context.Request["MobileType"];

            string code = "111";
            string msg = "fail";







            string sign = AdinZ.Common.EncryAndDecry.EncryMd5(MchNo+Phone+Fee+OrderId+KEY,true,true);
            if (sign.Equals(Sign))
            {
                int UID = 0;
                try
                {
                    UID = int.Parse(OrderId.Substring(OrderId.Length - 5, 5));
                    if (!DB.isOrdID(UID, MchNo))
                    {
                        if (DB.Recharge(UID, int.Parse(Fee) * 100, "android",GetMobileType(MobileType) + "用户(" + Phone + ")充值" + Fee + "元", MchNo))
                        {
                            code = "000"; msg = "success";
                        }
                        else
                        {
                            code = "401"; msg = "fail";
                        }
                    }
                    else
                    {
                        code = "403"; msg = "fail";
                    }
                }
                catch { code = "111"; msg = "fail"; }
            }
            else { code = "555"; msg = "fail"; }
            context.Response.Write(code + "~" + msg+"~0~0");
        }

        private static string GetMobileType(string type)
        {
            if (type.Equals("DX"))
            {
                return "电信";
            }
            else if (type.Equals("LT"))
            {
                return "联通";
            }
            else if (type.Equals("YD"))
            {
                return "移动";
            }
            else
            {
                return "未知";
            }
        }


    }
}
